const { Router } = require("express");
const Partment = require("../model/Partment");

const router = new Router();

router.use(require("../middleware/auth.js"));
//添加
router.post("/add", async (req, res) => {
  let time = Date.now();
  const { name } = req.body;
  Partment.create({ name, time });
  res.status(200).json({ message: "增加成功" });
});
//查询全部
router.get("/all", async (req, res) => {
  let result = await Partment.find();
  res.status(200).json({ message: "查询成功", data: result });
});
//条件查询
router.get("/search", async (req, res) => {
  const { ...data } = req.query;
  // console.log(data);
  let result = await Partment.find({ ...data });
  res.status(200).json({ msg: "查询成功", list: result });
});
//停用
router.get("/stop", async (req, res) => {
  const { id, type } = req.query;
  await Partment.findByIdAndUpdate({ _id: id }, { type });
  res.status(200).json({ msg: "修改成功" });
});
//删除
router.get("/delete", async (req, res) => {
  const { id } = req.query;
  await Partment.findByIdAndDelete({ _id: id });
  res.status(200).json({ msg: "删除成功" });
});
//部门修改
router.get("/revise", async (req, res) => {
  const { id, name } = req.query;
  await Partment.findByIdAndUpdate({ _id: id }, { name });
  res.status(200).json({ msg: "修改成功" });
});
module.exports = router;
